package tech.xiaozai.rbac.security.authentication;

import cn.hutool.json.JSONUtil;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import tech.xiaozai.common.restful.Error;
import tech.xiaozai.common.restful.RestResp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-09-23 11:30
 */
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        response.setContentType("application/json;charset=utf-8");
        RestResp error = RestResp.error(Error.BUSINESS_ERROR);
        String jsonStr = JSONUtil.toJsonStr(error);
        response.getWriter().write(jsonStr);
        response.getWriter().flush();
    }
}
